//
//  HJCommentCell.m
//  project
//
//  Created by yefei on 2019/7/22.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJCommentCell.h"
#import "HJVideoModel.h"
#import "YFJourneyModel.h"

@interface HJCommentCell ()<UITableViewDelegate,UITableViewDataSource>

/**
 头像
 */
@property (nonatomic,strong) UIImageView * avatorImageView;

/**
 名字
 */
@property (nonatomic,strong) UILabel * nameLabel;

/**
 内容
 */
@property (nonatomic,strong) UILabel * contentLabel;
/**
 时间
 */
@property (nonatomic,strong) UILabel * timeLabel;
/**
 回复
 */
@property (nonatomic,strong) UILabel * replyLabel;

/**
 回复的tableView
 */
@property (nonatomic,strong) UITableView * replayTable;

@end

@implementation HJCommentCell

- (void)awakeFromNib {
    [super awakeFromNib];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.avatorImageView];
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.timeLabel];
        [self.contentView addSubview:self.replyLabel];
        
        [self.contentView addSubview:self.replayTable];
    }
    return self;
}
-(UIImageView *)avatorImageView {
    if (!_avatorImageView) {
        _avatorImageView = [UIImageView new];
        _avatorImageView.image = [UIImage imageNamed:@"img_mydongtai_defualt_avatar"];
    }
    return _avatorImageView;
}

-(UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont fontWithName:PingFangSC_Medium size:15];
        _nameLabel.textColor = [UIColor colorWithHex:0x808080];
    }
    return _nameLabel;
}

-(UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [UILabel new];
        _contentLabel.font = [UIFont fontWithName:PingFangSC_Medium size:15];
        _contentLabel.textColor = [UIColor colorWithHex:0xffffff];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}
-(UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.font = [UIFont fontWithName:PingFangSC_Medium size:13];
        _timeLabel.textColor = [UIColor colorWithHex:0x808080];
    }
    return _timeLabel;
}
-(UILabel *)replyLabel {
    if (!_replyLabel) {
        _replyLabel = [UILabel new];
        _replyLabel.font = [UIFont fontWithName:PingFangSC_Medium size:13];
        _replyLabel.textColor = [UIColor colorWithHex:0xFF2400];
        _replyLabel.text = @"回复";
    }
    return _replyLabel;
}
-(UITableView *)replayTable {
    if (!_replayTable) {
        _replayTable = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _replayTable.scrollEnabled = NO;
        _replayTable.backgroundColor = [UIColor clearColor];
        _replayTable.separatorStyle= UITableViewCellSeparatorStyleNone;
        _replayTable.dataSource = self;
        _replayTable.delegate = self;
    }
    return _replayTable;
}
-(void)layoutSubviews {
    [super layoutSubviews];
    
    @weakify(self)
    [self.avatorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.left.equalTo(@15);
        make.size.mas_equalTo(CGSizeMake(40, 40));
        [self.avatorImageView rounded:UIRectCornerAllCorners radius:20];
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.equalTo(self.avatorImageView.mas_top);
        make.left.equalTo(self.avatorImageView.mas_right).offset(10);
        make.height.mas_equalTo(15);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
        make.left.equalTo(self.avatorImageView.mas_right).offset(10);
        make.right.equalTo(self.contentView.mas_right).inset(42);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
        make.left.equalTo(self.avatorImageView.mas_right).offset(10);
        make.height.mas_equalTo(13);
    }];
    
    [self.replyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.left.equalTo(self.timeLabel.mas_right).offset(10);
        make.centerY.equalTo(self.timeLabel);
        make.height.mas_equalTo(13);
    }];
}
#pragma mark-- 评论
-(void)setCommentModel:(HJCommentModel *)commentModel {
    _commentModel = commentModel;
    [self.avatorImageView sd_setImageWithURL:[NSURL URLWithString:commentModel.formUser.avatar]];
    self.nameLabel.text = commentModel.formUser.nickName;
    self.contentLabel.attributedText = commentModel.contentAttribute;
    self.timeLabel.text = commentModel.time;
    self.replyLabel.hidden = YES;
    
#pragma mark --- 回复数据
    ///table frame 重组
    CGFloat replyTableHeight = commentModel.cellHeight;
    if (_commentModel.reviewList.count>0) {
        replyTableHeight = commentModel.replyHeight;
        [self.replayTable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeLabel.mas_bottom).offset(10);
            make.left.right.equalTo(@0);
            make.height.mas_equalTo(replyTableHeight);
        }];
        [self.replayTable reloadData];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _commentModel.reviewList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * replayIdentifier = @"replayCommentCell";
    HJReplyCommentCell * replayCell= [tableView cellForRowAtIndexPath:indexPath];
    if (!replayCell) {
        replayCell = [[HJReplyCommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:replayIdentifier];
    }
    replayCell.replyCommentModel = _commentModel.reviewList[indexPath.row];
    return replayCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return _commentModel.reviewList[indexPath.row].cellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.CommentDidClickBlock) {
        self.CommentDidClickBlock(_commentModel.reviewList[indexPath.row],self.commentIndex);
    }
}


@end

@interface HJReplyCommentCell ()

/**
 头像
 */
@property (nonatomic,strong) UIImageView * avatorImageView;

/**
 名字
 */
@property (nonatomic,strong) UILabel * nameLabel;

/**
 内容
 */
@property (nonatomic,strong) UILabel * contentLabel;
/**
 时间
 */
@property (nonatomic,strong) UILabel * timeLabel;

@end

@implementation HJReplyCommentCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self.contentView addSubview:self.avatorImageView];
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.timeLabel];
    }
    return self;
}
-(UIImageView *)avatorImageView {
    if (!_avatorImageView) {
        _avatorImageView = [UIImageView new];
        _avatorImageView.image = [UIImage imageNamed:@"img_mydongtai_defualt_avatar"];
    }
    return _avatorImageView;
}

-(UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont fontWithName:PingFangSC_Medium size:15];
        _nameLabel.textColor = [UIColor colorWithHex:0x808080];
    }
    return _nameLabel;
}

-(UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [UILabel new];
        _contentLabel.font = [UIFont fontWithName:PingFangSC_Medium size:15];
        _contentLabel.textColor = [UIColor colorWithHex:0xffffff];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}
-(UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.font = [UIFont fontWithName:PingFangSC_Medium size:13];
        _timeLabel.textColor = [UIColor colorWithHex:0x808080];
    }
    return _timeLabel;
}
#pragma mark -- 更新UI布局
-(void)updateLayoutUI {
    @weakify(self)
    [self.avatorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.equalTo(@15);
        make.left.equalTo(@70);
        make.size.mas_equalTo(CGSizeMake(35, 35));
        [self.avatorImageView rounded:UIRectCornerAllCorners radius:17.5];
    }];

    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.equalTo(self.avatorImageView.mas_top);
        make.left.equalTo(self.avatorImageView.mas_right).offset(10);
        make.height.mas_equalTo(15);
    }];

    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
        make.left.equalTo(self.avatorImageView.mas_right).offset(10);
        make.right.equalTo(self.contentView.mas_right).inset(42);
    }];

    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
        make.left.equalTo(self.avatorImageView.mas_right).offset(10);
        make.height.mas_equalTo(13);
    }];
}
-(void)layoutSubviews {
    [super layoutSubviews];
    [self updateLayoutUI];
}

-(void)setReplyCommentModel:(HJCommentModel *)replyCommentModel {
    _replyCommentModel = replyCommentModel;
    [self.avatorImageView sd_setImageWithURL:[NSURL URLWithString:replyCommentModel.formUser.avatar]];
    self.nameLabel.text = replyCommentModel.formUser.nickName;
    self.contentLabel.attributedText = replyCommentModel.contentAttribute;
    self.timeLabel.text = replyCommentModel.time;
}
@end
